#pragma once
#include <iostream>
#include <string>
#include<memory>
#include<jsoncpp/json/json.h>

const std::string sep="\r\n";
// {json} -> len\r\n{json}\r\n
bool Encode(std::string &message)
{
    if(message.size()==0)return false;// 空消息不编码
    if(message.find(sep)!=std::string::npos) return false;// 不能包含分隔符
    std::string packge=std::to_string(message.size())+sep+message+sep;// 打包
    message=packge;// 赋值
    return true;
}

// len\r\n{json}\r\n -> {json}
bool Decode(std::string &message,std::string *content)
{
    auto pos=message.find(sep);// 找到分隔符的位置
    if(pos==std::string::npos) return false;//   没有找到分隔符
    std::string len_str=message.substr(0,pos);// 取出包的长度字符串
    int len=std::stoi(len_str);// 取出包的长度
    if(len_str.size()+sep.size()+len+sep.size()>message.size()) return false; // 判断长度是否不够
    *content=message.substr(len_str.size()+sep.size(),len);// 取出json字符串
    message=message.substr(len_str.size()+sep.size()+len+sep.size());// 去掉拿去的包的剩余部分
    return true;
}
class request
{
public:
    request(): _x(0), _y(0), _oper(0)
    {}
    request(int x, int y, char oper) : _x(x), _y(y), _oper(oper)
    {
    }
    // 序列化
    bool Serialization(std::string& out_string)
    {
        Json::Value root;
        root["x"]= _x;
        root["y"]=_y;
        root["oper"]=_oper;

        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root,&ss);
        out_string=ss.str();
        return true;

    }
    // 反序列化
    bool deserialization(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful=reader.parse(in_string,root);
        if(!parsingSuccessful)
            return false;
        _x=root["x"].asInt();
        _y=root["y"].asInt();
        _oper=root["oper"].asInt();
        return true;
    }
    void print()
    {
        std::cout<<_x<<std::endl;
        std::cout<<_y<<std::endl;
        std::cout<<_oper<<std::endl;
    }

    int X() const{return _x;}
    int Y() const{return _y;}    
    char Oper() const{return _oper;}

private:
    int _x;
    int _y;
    char _oper; // 加减乘除
};

class response
{
public:
    response(): _result(0), _code(0)
    {}
    response(int result, int code) : _result(result), _code(code)
    {
    }
    // 序列化
    bool Serialization(std::string& out_string)
    {
        Json::Value root;
        root["result"]=_result;
        root["code"]=_code;

        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root,&ss);
        out_string=ss.str();
        return true;
    }
    // 反序列化
    bool deserialization(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful=reader.parse(in_string,root);
        if(!parsingSuccessful)
            return false;
        _result=root["result"].asInt();
        _code=root["code"].asInt();
        return true;
    }
    int Result() const{return _result;}
    int Code() const{return _code;}
    int setResult(int result){_result=result;return _result;}
    int setCode(int code){_code=code;return _code;}
private:
    int _result; // 结果
    int _code;   // 错误码:0 1 2
};